package android.slc.extras.component.tree.ui

import android.os.Bundle
import android.slc.appbase.ui.vd.BaseAppVmBox
import android.slc.appbase.utils.AppIntentUtils
import android.slc.extras.component.tree.config.ConstantsTree
import android.slc.extras.component.tree.entity.SlcTree
import com.blankj.utilcode.util.CollectionUtils

/**
 * 多选盒子数据存储
 */
abstract class TreeMultipleShelfVmBox<T : SlcTree> : BaseAppVmBox() {
    /**
     * 选择的集合
     */
    protected val mSelectListMap: MutableMap<String, MutableList<T>> = mutableMapOf()

    /**
     * 单独列表的集合
     */
    val mSelectNotParentList = mutableListOf<T>()

    /**
     * 是否有选择改变
     */
    private var mSelectChange = false

    /**
     * 初始化选择数据
     */
    fun initSelectData(bundle: Bundle?) {
        //获取选择的数据
        bundle?.getSerializable(ConstantsTree.KEY_DATA_BY_SELECT)?.let {
            val dataBySelect = it as MutableList<*>
            CollectionUtils.forAllDo(dataBySelect) { _, item ->
                if (checkTreeEntityType(item as SlcTree)) {
                    //根据父节点id来进行分组
                    val treeList =
                        this@TreeMultipleShelfVmBox.mSelectListMap[item.parentTId]
                            ?: let valueList@{
                                val treeList = mutableListOf<T>()
                                this@TreeMultipleShelfVmBox.mSelectListMap[item.parentTId] =
                                    treeList
                                return@valueList treeList
                            }
                    treeList.add(item as T)
                }
            }
            bundle.remove(ConstantsTree.KEY_DATA_BY_SELECT)
        }
        bundle?.getSerializable(ConstantsTree.KEY_DATA_BY_SELECT_NOT_PARENT)?.let {
            val dataBySelectNotSelect = it as MutableList<*>
            CollectionUtils.forAllDo(dataBySelectNotSelect) { _, item ->
                mSelectNotParentList.add(item as T)
            }
        }
    }

    /**
     * 通知选择改变
     */
    open fun notifySelectChange() {
        this.mSelectChange = true
    }

    open fun handlerCompleteSelectChange() {
        this.mSelectChange = false
    }

    fun isSelectChange(): Boolean {
        return this.mSelectChange
    }

    /**
     * 根据树id获取选择列表
     */
    fun getSelectListByTreeId(treeId: String): MutableList<T> {
        var selectListByTreeId = this.mSelectListMap[treeId]
        if (selectListByTreeId == null) {
            selectListByTreeId = mutableListOf()
            this.mSelectListMap[treeId] = selectListByTreeId
        }
        return selectListByTreeId
    }

    /**
     * 设置选中的数据
     */
    /*fun putSelectDataList(treeId: Serializable, selectInfoList: MutableList<T>) {
        this.selectChange = true
        this.mSelectListMap[treeId] = selectInfoList
    }*/

    /**
     * 获取选中的数据转成list
     */
    private fun getSelectData2List(): ArrayList<T> {
        val selectUser = arrayListOf<T>()
        CollectionUtils.forAllDo(this.mSelectListMap.values) { _, item ->
            selectUser.addAll(item)
        }
        return CollectionUtils.union(selectUser, mSelectNotParentList) as ArrayList<T>
    }

    /**
     * 检查树实体类型
     */
    protected abstract fun checkTreeEntityType(item: Any): Boolean

    /**
     * 完成
     */
    fun complete() {
        handlerCompleteSelectChange()
        fillResult(AppIntentUtils.getBundleByData(this.getSelectData2List()))
    }
}